﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using MonkeyShop.Admin.Biz.Data;

namespace MonkeyShop.Admin.Biz
{
    public class AdminCategoryBiz
    {
        /// <summary>
        /// Gets the categories.
        /// </summary>
        /// <returns></returns>
        public IList<Category> GetCategories()
        {
            using (var db = new MonkeyShopAdminEntities())
            {
                var categories = (
                        from c in db.Categories
                        select c
                    ).ToList();

                return categories;
            }
        }

        #region CRUD

        /// <summary>
        /// Creates the specified category.
        /// </summary>
        /// <param name="category">The category.</param>
        public void Create(Category category)
        {
            if (category == null) return;
            if (string.IsNullOrWhiteSpace(category.Name)) return;

            using (var db = new MonkeyShopAdminEntities())
            {
                db.Categories.Add(category);
                db.SaveChanges();
            }
        }

        /// <summary>
        /// Updates the specified category.
        /// </summary>
        /// <param name="category">The category.</param>
        public void Update(Category category)
        {
            if (category == null) return;
            if (category.Id <= 0) return;
            if (string.IsNullOrWhiteSpace(category.Name)) return;

            // check category name is existing
            using (var db = new MonkeyShopAdminEntities())
            {
                var count = db.Categories.Count(c => c.Name == category.Name && c.ParentId == category.ParentId);
                if (count > 0)
                    throw new ApplicationException("CategoryWasExisted");

                db.Categories.Attach(category);
                db.Entry(category).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        /// <summary>
        /// Deletes the specified category identifier.
        /// </summary>
        /// <param name="categoryId">The category identifier.</param>
        /// <exception cref="System.ApplicationException">
        /// CategoryCannotDelete
        /// or
        /// CategoryCannotDelete
        /// </exception>
        public void Delete(int categoryId)
        {
            if (categoryId <= 0) return;
            using (var db = new MonkeyShopAdminEntities())
            {
                // check if has childs
                var count = db.Categories.Count(c => c.ParentId == categoryId);
                if (count > 0) throw new ApplicationException("CategoryCannotDelete");

                var category = db.Categories.FirstOrDefault(c => c.Id == categoryId);
                if (category == null) throw new ApplicationException("CategoryCannotDelete");

                // delete product2category
                var productToCategories = db.ProductToCategories.Where(p => p.CategoryId == categoryId);
                foreach (var productToCategory in productToCategories)
                {
                    db.Entry(productToCategory).State = EntityState.Deleted;
                }

                db.Entry(category).State = EntityState.Deleted;

                // commit changes
                db.SaveChanges();
            }
        }

        #endregion
    }
}
